//
//  PywTradePWDController.m
//  PywSdk
//
//  Created by smallwave on 16/5/23.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywTradePWDController.h"
#import "PywMasonry.h"
#import "UIImage+Pyw.h"
#import "PywPlatformMain.h"
#import "AppUtil.h"
#import "PywCipherUtils.h"
#import "ApiClientConst.h"


@interface SdkObj:NSObject<Paypwdprotocol>
@property (nonatomic,assign) UIViewController *controller;
- (instancetype)initWithController:(UIViewController *)controller;
@end
@implementation SdkObj
- (instancetype)initWithController:(UIViewController *)controller
{
    self = [super init];
    if (self) {
        self.controller = controller;
    }
    return self;
}
/**
 *  退出修改页面，返回个人中心
 */
- (void)doSdkExit
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.controller.navigationController popViewControllerAnimated:YES];
        [[NSNotificationCenter defaultCenter] postNotificationName:ReloadUserCenterData object:nil];
    });
    
}
@end

@interface PywTradePWDController ()<UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *webView;

@end

@implementation PywTradePWDController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.rightBarButtonItem = nil;
    self.title = @"交易密码";
    [self addWebView];
}



#pragma mark - privateMethods
- (void)addWebView{
    [self.view addSubview:self.webView];
    __weak typeof(self) weakSelf = self;
    [weakSelf.webView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.top.bottom.right.left.offset(0);
    }];
    [self postParamToJS];
    
}

/**
 *  发送请求添加Param参数
 */
- (void)postParamToJS
{
    
    NSString *param = [NSString stringWithFormat:@"%@|%@",[PywPlatformMain sharedPlatformMain].passport,[PywPlatformMain sharedPlatformMain].passport_pwd];
    NSData *paramData = [param dataUsingEncoding:NSUTF8StringEncoding];
    NSData *testData = [AppUtil encode:paramData];
    Byte *testByte = (Byte *)[testData bytes];
    //字段值是byte[] 形式传递
    NSMutableString *paramString = [[NSMutableString alloc] init];
    for (int i = 0; i < testData.length; i ++) {
        if (paramString.length > 0) {
            [paramString appendString:@","];
        }
        [paramString appendFormat:@"%d",testByte[i]];
    }
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:self.wap_set_paypwd_url]
                                                  cachePolicy:NSURLRequestReloadIgnoringCacheData
                                              timeoutInterval:20];
    NSMutableURLRequest *mutableRequest = [request mutableCopy];
    [mutableRequest addValue:paramString forHTTPHeaderField:@"param"];
    request = [mutableRequest copy];
    [self.webView loadRequest:request];
    
}



#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    //JS-->:window.sdkObj.doSdkExit 接收JS调用OC
    JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    context.exceptionHandler =
    ^(JSContext *context, JSValue *exceptionValue)
    {
        context.exception = exceptionValue;
        NSLog(@"%@", exceptionValue);
    };
    SdkObj *sdkObj = [[SdkObj alloc] initWithController:self];
    context[@"sdkObj"] = sdkObj;
    
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    return YES;
}

#pragma mark - setter & getter
- (UIWebView *)webView{
    if (!_webView) {
        _webView = [[UIWebView alloc] init];
        _webView.delegate = self;
        _webView.scalesPageToFit = YES;
    }
    return _webView;
}


@end
